/*******************************************************************************
File    : 02_NSS_Waves.do
Project : Bank Expansion and Moneylender Interest Rates - RDD Evidence from India
Purpose : Build AIDIS wave analysis datasets (2003/2013/2019) by merging
          raw NSS/AIDIS files with MOF–Census mapping files and constructing
          key outcome variables.
Authors : Briones L., Chiseliov V., Gushchin E., Narayanaswamy G.
Date    : 09 Feb 2026
*******************************************************************************/

*------------------------------------------------------------------------------
* Assumptions
* - Master do-file already sets cd to project root ($PROJROOT).
* - Mapping files already exist:
*     Data\MOF_Census_NSS_59.dta
*     Data\MOF_Census_NSS_70.dta
*     Data\MOF_Census_NSS_77.dta
*------------------------------------------------------------------------------

di as txt ">>> Building NSS/AIDIS wave datasets..."

*==============================================================================
* 1. AIDIS I (2003): AllIndiaI.dta
*==============================================================================

di as txt ">>> AIDIS I (2003): building Data\AllIndiaI.dta"

use "Data\59_round\Visit1_Block15pt2.dta", clear

* Create geographic identifier and merge
gen NSS_59_Codes = State + District
merge m:1 NSS_59_Codes using "Data\MOF_Census_NSS_59.dta"
drop if _merge != 3
rename _merge _merge2

* Prepare treatment and outcome variables
gen log_i = log(B15_2_q10)

* Standardize outcome variable
summarize log_i
generate stdlog_i = (log_i - r(mean)) / r(sd)

* Ensure numeric identifiers
destring State District, replace
destring NSS_59_Codes, replace

* Save final 2003 dataset
save "Data\AllIndiaI.dta", replace


*==============================================================================
* 2. AIDIS II (2013): AllIndiaII.dta
*==============================================================================

di as txt ">>> AIDIS II (2013): building Data\AllIndiaII.dta"

use "Data\70_round\Visit1_Block 14.dta", clear

* Merge with mapping data
rename State_District NSS_70_Codes
merge m:1 NSS_70_Codes using "Data\MOF_Census_NSS_70.dta"
drop if _merge != 3

* Keep only moneylender loans and prepare analysis variables
drop if B_Ratio_Dist_Ex == .

* Transform outcome variable
gen log_i = log(b14_q10)
summarize log_i
generate stdlog_i = (log_i - r(mean)) / r(sd)

* Ensure numeric identifiers
destring State District, replace
destring NSS_70_Codes, replace

destring b14_q4 b14_q6 b14_q8 b14_q11, replace

label define b14_q4 1 "loan remaining unpaid on 30.06.12" 2 "loan taken during 1.7.12 to date of survey"
label define b14_q6  ///
	1	"government" ///
	2	"co-operative society/bank" ///
	3	"commercial bank incl. regional rural bank" ///
	4	"insurance" ///
	5	"provident fund" ///
	6	"financial corporation/institution" ///
	7	"financial company" ///
	8	"self-help group-bank linked (SHG-BL)" ///
	9	"others" ///
	10	"self-help group, non-banking financial companies (SHG-NBFC)" ///
	11	"other institutional agencies" ///
	12	"landlord" ///
	13	"agricultural moneylender" ///
	14	"professional moneylender" ///
	15	"input supplier" ///
	16	"relatives and friends" ///
	17	"doctors, lawyers & other professionals" ///

label define b14_q8 ///
	1 "short-term, pledged" ///
	2 "short-term, non-pledged" ///
	3 "medium term" ///
	4 "long term"
label define b14_q11 ///
	01 "capital expenditure: farm business" ///
	02 "current expenditure: farm business" ///
	03 "capital expenditure: non-farm business" ///
	04 "current expenditure: non-farm business" ///
	05 "litigation expenses" ///
	06 "repayment of debt" ///
	07 "financial investment" ///
	08 "education" ///
	09 "others" ///
	10 "medical treatment" ///
	11 "housing" ///
	12 "other household expenditure"

label values b14_q4 b14_q4
label values b14_q6 b14_q6
label values b14_q8 b14_q8
label values b14_q11 b14_q11


save "Data\AllIndiaII.dta", replace


*==============================================================================
* 3. AIDIS III (2019): AllIndiaIII.dta
*==============================================================================

di as txt ">>> AIDIS III (2019): building Data\AllIndiaIII.dta"

use "Data\77_round\Visit1 Level - 14 (Block 12) - particulars of cash loans payable.dta", clear

gen Weight_SS = MLT / 100  // Survey weight

* Prepare for merge with mapping data
rename State_District NSS_77_Codes
rename District District_code

* Merge with census data
merge m:1 NSS_77_Codes using "Data\MOF_Census_NSS_77.dta"
drop if _merge != 3
rename _merge _merge2

drop if B_Ratio_Dist_Ex == .  // Remove observations with missing running variable

* Interest rate transformations
sum b12q9
gen log_i = log(b12q9)  // Log interest rate

summarize log_i
gen stdlog_i = (log_i - r(mean)) / r(sd)  // Standardized log interest rate

* Ensure numeric geographic identifiers
destring State District_code, replace
destring NSS_77_Codes, replace

save "Data\AllIndiaIII.dta", replace


di as result ">>> NSS/AIDIS wave datasets created: AllIndiaI, AllIndiaII, AllIndiaIII"
exit
